import 'package:flame/flame.dart';
import 'package:flame/game.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'dart:ui' as ui;

import 'MyTank.dart';
import 'component/JoyStick.dart';

class GameFlutter extends StatelessWidget {
  GameFlutter({Key? key}) : super(key: key);
  final MyTank game = MyTank();
  @override
  Widget build(BuildContext context) {
    return Directionality(
        textDirection: TextDirection.ltr,
        child: Scaffold(
          appBar: AppBar(
            title: const Text('My Tank'),
            leading: IconButton(
              icon: Icon(Icons.arrow_back),
              onPressed: () => Get.back(result: {'success': 'tank'}),
            ),
            backgroundColor: Color.fromRGBO(154, 223, 217, 0.961),
          ),
          body: Stack(
            children: [
              FutureBuilder(
                  future: loadAssets(),
                  initialData: [],
                  builder: (ctx, snapShot) {
                    if (snapShot.data?.isEmpty ?? true) {
                      return const Center(
                        child: LinearProgressIndicator(
                          color: Colors.blue,
                        ),
                      );
                    }
                    return GameWidget(game: game);
                  }),
              Column(
                children: [
                  Spacer(),
                  Row(
                    children: [
                      SizedBox(width: 100),
                      JoyStick(
                          onJoyStickListener: (Offset details) =>
                              game.JoypadChange(details)),
                      Spacer(),
                      FireButton(onTap: () {
                        game.onFireStart();
                      }),
                      SizedBox(width: 100),
                      // Align(
                      //   alignment: Alignment.bottomRight,
                      //   child: Padding(
                      //     padding: EdgeInsets.only(right: 100),
                      //     child: FireButton(onTap: () {}),
                      //   ),
                      // ),
                    ],
                  ),
                  SizedBox(height: 100),
                ],
              )
            ],
          ),
        ));
  }
}

Future<List<ui.Image>> loadAssets() {
  return Flame.images.loadAll([
    'new_map.webp',
    'tank/t_body_blue.webp',
    'tank/t_turret_blue.webp',
    'tank/t_body_green.webp',
    'tank/t_turret_green.webp',
    'tank/t_body_sand.webp',
    'tank/t_turret_sand.webp',
    'tank/bullet_blue.webp',
    'tank/bullet_green.webp',
    'tank/bullet_sand.webp',
    'explosion/explosion1.webp',
    'explosion/explosion2.webp',
    'explosion/explosion3.webp',
    'explosion/explosion4.webp',
    'explosion/explosion5.webp',
  ]);
}

class FireButton extends StatelessWidget {
  FireButton({Key? key, required this.onTap}) : super(key: key);
  final void Function() onTap;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 64,
      width: 64,
      child: Container(
        decoration: BoxDecoration(borderRadius: BorderRadius.circular(32)),
        child: GestureDetector(
          child: Container(
            decoration: BoxDecoration(
              color: Color(0x88ffffff),
              borderRadius: BorderRadius.circular(32),
            ),
          ),
          onTap: onTap,
        ),
      ),
    );
  }
}
